package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.Permission;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 根据id获取权限
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> map = permissionService.findById(id);
            return new Result(true,"获取权限列表成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取权限列表失败");
        }
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @RequestMapping("add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true,"新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败");
        }
    }

    /**
     * 分页查询权限列表
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.findPage(queryPageBean);
        return pageResult;
    }
}
